package com.sizu.web;


import com.sizu.common.dto.CategoryDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.CgCategory;
import com.sizu.service.ICgCategoryService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author 高旌洋
 * @since 2020-03-25
 */
@RestController
@RequestMapping("category/cgCategory")
public class CgCategoryController {

    @Autowired
    private ICgCategoryService categoryService;

    /**
     * 查询 Category表
     * @return
     */
    @GetMapping
    public ResultEntity findAll(){
        List<CgCategory> categoryList = categoryService.selectList(null);
        return ResultEntity.ok(categoryList);
    }

    /**
     * 根据ID查询Category实例
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResultEntity findById(@RequestParam("id") Integer id){
        CgCategory category = categoryService.selectById(id);
        return ResultEntity.ok(category);
    }

    /**
     * 添加Category
     * @param categoryDto
     * @return
     */
    @PostMapping(value = "/save")
    public ResultEntity save(@RequestBody CategoryDto categoryDto){
        CgCategory cgCategory=new CgCategory();
        BeanUtils.copyProperties(categoryDto,cgCategory);
        categoryService.insert(cgCategory);
        return ResultEntity.ok("200","添加成功",null);
    }

    /**
     * 根据ID删除Category实例
     * @param id
     * @return
     */
    @GetMapping(value = "/deleteById")
    public ResultEntity deleteById(Integer id){
        categoryService.deleteById(id);
        return ResultEntity.ok("200","删除成功",null);
    }
}
